{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nStep 1: from output of evaluate qualification.py: worker_id and int percentage value\\nStep 2: open worker csv file (downloaded from turk) and\\n    stat_dict = {worker_id: eval_score}\\n    for each cell in csv:\\n        if cellworker_id in stat_dict:\\n            update the qual column with stat_dict[cell_worker_id]\\n        else copy all rows\\nStep 3: now upload this csv\\nStep 4: for the task add this qualification task eval score > 80\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "Step 1: from output of evaluate qualification.py: worker_id and int percentage value\n",
    "Step 2: open worker csv file (downloaded from turk) and\n",
    "    stat_dict = {worker_id: eval_score}\n",
    "    for each cell in csv:\n",
    "        if cellworker_id in stat_dict:\n",
    "            update the qual column with stat_dict[cell_worker_id]\n",
    "        else copy all rows\n",
    "Step 3: now upload this csv\n",
    "Step 4: for the task add this qualification task eval score > 80\n",
    "'''\n",
    "# also note that: data/qualified_workers.txt has list of all worker ids that have been qualified\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [[\"fill the big hole with water\", \"fill the <span style='background-color: #FFFF00'>big hole</span> with water\", \"fill\", \"reference_object\"],\n",
    "     [\"make a copy of that\", \"make a copy of <span style='background-color: #FFFF00'>that</span>\", \"copy\", \"reference_object\"],\n",
    "     [\"make a copy of that behind me\", \"make a copy of that <span style='background-color: #FFFF00'>behind me</span>\", \"copy\", \"location\"],\n",
    "     [\"spawn enderman over there\", \"spawn <span style='background-color: #FFFF00'>enderman</span> over there\", \"spawn\", \"reference_object\"],\n",
    "     [\"spawn enderman over there\", \"spawn enderman <span style='background-color: #FFFF00'>over there</span>\", \"spawn\", \"location\"],\n",
    "     [\"destroy the big house\", \"destroy the <span style='background-color: #FFFF00'>big house</span>\", \"destroy\", \"reference_object\"],\n",
    "     [\"go behind the sheep\", \"go <span style='background-color: #FFFF00'>behind the sheep</span>\", \"move\", \"location\"],\n",
    "     [\"dig a 10 x 10 hole there\", \"dig a <span style='background-color: #FFFF00'>10 x 10 hole</span> there\", \"dig\", \"schematic\"],\n",
    "     [\"dig a 10 x 10 hole there\", \"dig a 10 x 10 hole <span style='background-color: #FFFF00'>there</span>\", \"dig\", \"location\"],\n",
    "     [\"complete the village\", \"complete the <span style='background-color: #FFFF00'>village</span>\", \"freebuild\", \"reference_object\"],\n",
    "     [\"do a dance over there\", \"do a dance <span style='background-color: #FFFF00'>over there</span>\", \"dance\", \"location\"],\n",
    "     [\"build a green house there\", \"build a green house <span style='background-color: #FFFF00'>there</span>\", \"build\", \"location\"],\n",
    "     [\"build a green house there\", \"build a <span style='background-color: #FFFF00'>green house</span> there\", \"build\", \"schematic\"],\n",
    "     [\"go around the house three times\", \"go around <span style='background-color: #FFFF00'>the house</span> three times\", \"move\", \"location\"],\n",
    "     [\"that looks nice\", \"that looks <span style='background-color: #FFFF00'>nice</span>\", \"tag\", \"tag_val\"],\n",
    "     [\"that looks nice\", \"<span style='background-color: #FFFF00'>that</span> looks nice\", \"tag\", \"filters\"]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('highlight_example.txt', 'w') as f:\n",
    "    for line in x:\n",
    "        words = \"\\t\".join(line)\n",
    "        f.write(words+\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "old_stats = {}\n",
    "with open('data/qual_test_workers/all_qualified_workers.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        worker_id, score = line.strip().split(\"\\t\")\n",
    "        if worker_id in old_stats:\n",
    "            print(worker_id)\n",
    "        old_stats[worker_id] = score\n",
    "print(len(old_stats.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 workers had given us data already\n"
     ]
    }
   ],
   "source": [
    "worker_stats = {}\n",
    "cnt = 0\n",
    "with open('data/qual_test_workers/second_500_workers.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        worker_id, score = line.strip().split(\"\\t\")\n",
    "        if worker_id in old_stats:\n",
    "            cnt += 1\n",
    "            print(\"%r has already given us data before. Old score: %r new score: %r\"%(worker_id, old_stats[worker_id], score))\n",
    "        worker_stats[worker_id] = score\n",
    "print(\"%r workers had given us data already\"%(cnt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Written :500 new lines\n"
     ]
    }
   ],
   "source": [
    "# write out all unique workers\n",
    "cnt = 0 \n",
    "with open('data/qual_test_workers/all_qualified_workers.txt', 'a') as f:\n",
    "    for k, v in worker_stats.items():\n",
    "        if k not in old_stats:\n",
    "            cnt += 1\n",
    "            f.write(k +\"\\t\"+v+\"\\n\")\n",
    "print(\"Written :%r new lines\" % (cnt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31\n"
     ]
    }
   ],
   "source": [
    "perfect_score =0\n",
    "with open('data/qual_test_workers/all_qualified_workers.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        score = line.strip().split(\"\\t\")[1]\n",
    "        if score =='100':\n",
    "            perfect_score+= 1\n",
    "print(perfect_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "print(len(worker_stats.keys()))\n",
    "print(len(old_stats.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "500\n"
     ]
    }
   ],
   "source": [
    "from tempfile import NamedTemporaryFile\n",
    "import shutil\n",
    "import csv\n",
    "\n",
    "filename = '/Users/kavyasrinet/Downloads/all_workers.csv'\n",
    "tempfile = 'data/updated_workers.csv'\n",
    "\n",
    "with open(filename, 'r') as csvfile, open(tempfile, 'w') as outfile:\n",
    "    reader = csv.reader(csvfile)\n",
    "    writer = csv.writer(outfile)\n",
    "    cnt, cnt2 = 0, 0\n",
    "    for i, row in enumerate(reader):\n",
    "        # remove all old validated workers\n",
    "        if (row[13] or row[14]) and i!=0:\n",
    "            cnt += 1\n",
    "            # row[13] = None\n",
    "        w_id = row[0]\n",
    "        if w_id in worker_stats:\n",
    "            cnt2 += 1\n",
    "            # print('updating row', row)\n",
    "            row[14] = worker_stats[w_id]\n",
    "        writer.writerow(row)\n",
    "print(cnt)\n",
    "print(cnt2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "515\n"
     ]
    }
   ],
   "source": [
    "cnt, cnt2 = 0, 0\n",
    "with open('data/round2_updated_workers.csv', 'r') as f:\n",
    "    reader = csv.reader(f)\n",
    "    cnt  = 0\n",
    "    for row in reader:\n",
    "        if row[13]:\n",
    "            cnt += 1\n",
    "        if row[14]:\n",
    "            #print(row[14])\n",
    "            cnt2 += 1\n",
    "            \n",
    "print(cnt)\n",
    "print(cnt2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0\n",
      "{'66': 65, '33': 144, '0': 274, '100': 31}\n"
     ]
    }
   ],
   "source": [
    "data_stats = {}\n",
    "with open('data/mturk_workers_folder/updated_qual_workers.csv', 'r') as csvfile:\n",
    "    reader = csv.reader(csvfile)\n",
    "    cnt, cnt2 = 0, 0\n",
    "    for i, row in enumerate(reader):\n",
    "        # remove all old validated workers\n",
    "        if row[13] and i!=0:\n",
    "            cnt +=1 \n",
    "        if row[14] and i!= 0:\n",
    "            data_stats[row[14]] = data_stats.get(row[14], 0) + 1\n",
    "print(cnt, cnt2)\n",
    "print(data_stats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
